package images_api

import (
	"github.com/gin-gonic/gin"
	"zswei.xyz/manager/global"
	"zswei.xyz/manager/models/res"
	"zswei.xyz/manager/service"
	"zswei.xyz/manager/service/images_ser"
)

// FileUploadDataView 上传文件【批量】
//
//	@Tags			文件管理
//	@Summary		上传文件【批量】
//	@Description	上传文件【批量】接口
//	@Param			file	formData	file	true	"上传文件参数"
//	@Router			/v2/image/upload [post]
//	@Produce		json
//	@Success		200	{object}	res.Response{}
func (ImagesApi) FileUploadDataView(c *gin.Context) {
	form, err := c.MultipartForm() // 接收多个上传文件
	if err != nil {
		res.FailWithDesc("请选择上传文件", c)
		return
	}
	fileList, ok := form.File["files"] // 转换上传多个文件
	if !ok {
		res.FailWithDesc("请选择上传文件", c)
		return
	}
	var resList []images_ser.FileUploadResponse // 定义上传文件列表信息

	for _, file := range fileList {
		// 上传文件
		uploadService := service.ServiceApp.ImageService.ImageUploadService(file)

		// 如果错误了
		if !uploadService.IsSuccess {
			resList = append(resList, uploadService)
			continue
		}

		// 文件存放到本地文件夹中
		if global.Config.Upload.UploadAdd == 0 {
			err = c.SaveUploadedFile(file, uploadService.FileName) // 保存图片
			if err != nil {
				resList = append(resList, images_ser.FileUploadResponse{
					FileName:  file.Filename,
					IsSuccess: false,
					Desc:      err.Error(),
				})
				continue
			}
		}

		// 兜底信息
		resList = append(resList, uploadService)
	}

	res.OkWithData(resList, c)
}
